//
//  CarOrderBasicInfoCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/11/2.
//

import UIKit

class CarOrderBasicInfoCell: UITableViewCell {
    var tabelView: BaseTableView!
    var model: OrderInfoModel!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.selectionStyle = .none
        self.clipsToBounds = true
        
        self.initUI()
    }
    
    func initUI(){
        
        self.tabelView = BaseTableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 200), style: .plain)
        self.tabelView.backgroundColor = .white
        self.tabelView.delegate = self
        self.tabelView.dataSource = self
        self.tabelView.separatorStyle = .none
        self.tabelView.rowHeight = UITableViewAutomaticDimension
        self.tabelView.register(BasicSonTableViewCell.self, forCellReuseIdentifier: "BasicSonTableViewCell")
        self.tabelView.estimatedRowHeight = 200
        self.tabelView.isScrollEnabled = false
        self.tabelView.layer.cornerRadius = 8
        self.tabelView.layer.masksToBounds = true
        self.contentView.addSubview(self.tabelView)
        
        
        self.tabelView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.greaterThanOrEqualTo(215)
            make.bottom.equalTo(0)
        }
        
        
    }
    
    
    func setModel(model: OrderInfoModel){
        self.model = model
        //自适应高度
        self.tabelView.frame.size.height = 9999
        self.tabelView.reloadData()
        self.tabelView.layoutSubviews()
        self.tabelView.layoutIfNeeded()
        let tempH = self.tabelView.contentSize.height
        self.tabelView.snp.updateConstraints { make in
            make.height.greaterThanOrEqualTo(tempH + 10)
        }

        self.updateConstraints()
        self.layoutIfNeeded()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension CarOrderBasicInfoCell: UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if self.model?.couponCodeDTO?.couponName == nil && indexPath.row == 0{//没有优惠
            return 0.01
        }
        if self.model?.paymentTime ?? "" == "" && indexPath.row == 4{
            return 0.01
        }
      return  35
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "BasicSonTableViewCell", for: indexPath) as! BasicSonTableViewCell
        cell.contentBtn.setImage(nil, for: .normal)
        switch indexPath.row {
        case 0:
            cell.titleLab.text = MMLocalizedString("discounts", comment: "优惠")
            let allPrice = XTManageCenter.showPriceInComma(source: ((self.model?.couponCodeDTO?.reducePrice ?? 0).cleanZero))
            cell.contentBtn.setTitle(XTManageCenter.currentSign() + allPrice, for: .normal)
        case 1:
            cell.titleLab.text = MMLocalizedString("amount_actually_paid", comment: "实付金额")
            let allMoney = (self.model?.price ?? 0) - (self.model?.couponCodeDTO?.reducePrice ?? 0)
            let allPrice = XTManageCenter.showPriceInComma(source: allMoney.cleanZero)
            cell.contentBtn.setTitle(XTManageCenter.currentSign() + allPrice, for: .normal)
        case 2:
            cell.titleLab.text = MMLocalizedString("order_number", comment: "订单号")
            cell.contentBtn.setTitle(self.model?.orderNo ?? "", for: .normal)
            cell.contentBtn.setImage(UIImage(named: "icon_copy"), for: .normal)
            cell.contentBtn.semanticContentAttribute = .forceRightToLeft
        case 3:
            cell.titleLab.text = MMLocalizedString("order_time", comment: "订单时间")
            cell.contentBtn.setTitle(self.model?.createdTime ?? "", for: .normal)
        default:
            cell.titleLab.text = MMLocalizedString("time_payment", comment: "支付时间")
            cell.contentBtn.setTitle(self.model?.paymentTime ?? "", for: .normal)
        }
        return cell
    
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0.01))
        headerView.backgroundColor = .clear
        return headerView
    }
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0.01))
        return footerView
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 2{
            let past = UIPasteboard.general//系统自带的复制
            past.string = self.model?.orderNo ?? ""
            self.makeToast(message: MMLocalizedString("copy_successed", comment: "复制成功"))
        }
    }
    
}
